package com.example.administrator.mynote;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{


    /*默认显示 （添加 修改）
    * 两个图片的点击事件*/
    ImageView back, save, delete;
    EditText ed_content;
    TextView tv_name,tv_time;
    NoteSQLiteOpenHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        init();
        helper=new NoteSQLiteOpenHelper(this);
        /*默认显示*/
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        delete.setOnClickListener(this);
        tv_name.setText("添加记录");
        /*通过intent传递的值，判断若是修改操作，则显示：标题、时间、内容*/
        Intent intent=this.getIntent();
        int id =intent.getIntExtra("id",0);
        if (id>0){
            tv_name.setText("修改记录");
            ed_content.setText(intent.getStringExtra("content"));
            tv_time.setText(intent.getStringExtra("time"));
            tv_time.setVisibility(View.VISIBLE);
        }
    }

    private void init() {
        back = findViewById(R.id.note_record_image_back);
        save = findViewById(R.id.note_record_image_save);
        delete = findViewById(R.id.note_record_image_delete);
        ed_content = findViewById(R.id.note_record_ed_content);
        tv_name=findViewById(R.id.note_record_tv_name);
        tv_time=findViewById(R.id.note_record_tv_time);
    }


    /*图片点击事件 清空 添加 修改和返回*/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.note_record_image_back:
                finish();
            break;
            case R.id.note_record_image_delete:
                ed_content.setText("");
            break;
            case R.id.note_record_image_save:
                String content=ed_content.getText().toString();
                if (TextUtils.isEmpty(content)){
                    Toast.makeText(this,"内容不能为空",Toast.LENGTH_LONG).show();

                }else {
                    Intent intent=this.getIntent();
                    int id =intent.getIntExtra("id",0);
                    if (id>0){
                        if (helper.update(id,content,helper.getTime())){
                            setResult(2);
                            finish();
                            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        if (helper.insert(content,helper.getTime())){
                            setResult(2);
                            finish();
                            Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(this,"添加失败" ,Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            break;
            default:

            break;
        }
    }
}
